<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
     $this->display();
    }
        
    public function register(){
    	//添加用户
    	//用户名是否存在、口令是否正确、加密
    	$userModel=D('users');
        if($userModel->create()){
        	 if($_FILES['photo']['error']==0){
        	  $fileInfo=$this->img();
              // $photoinfo=$this->upload();
               $userModel->photo=$fileInfo;
    	     if($userModel->add()){
    		   $this->success('注册用户成功','index');

    	     }else{
    		   $this->error('注册用户失败');
    	     }
          }   
        }else{
    	$this->error($userModel->getError());
    }
    }
       public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
        //$upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub=false;
        // 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
        }else{// 上传成功
             return $info;
      // $this->success('上传成功！');
        }
    }
   function img(){	
				 $photoinfo=$this->upload();
				 $filePath='./Public/Uploads/photo/';
				 $oldFilename=$photoinfo['photo']['savename'];
				 $oldFullname=$filePath.$oldFilename;
				 $newFilename='thumb_'.$oldFilename;
				 $newFullname=$filePath.$newFilename;//这一堆，慢慢看吧
				 $image=new \Think\Image();//生成图片类

				 $image->open($oldFullname);//打开文件

				 $image->thumb(110,110)->save($newFullname);
			
				 return $newFilename;
		}

        
}
